global.FS = require('fs');
global.xiancheng = {};
var exec = require('child_process').exec;
global.goshell = async function(appPath){
    if(appPath && appPath != "" &&!xiancheng[appPath]){
        var worker = exec(appPath);
        xiancheng[appPath] = worker.pid ;
        worker.on('messageerror',function(data){
           
        });

        worker.on('exit',function(data){
            delete xiancheng[appPath];
            setTimeout(function(){
                var wenjian = null;
                try {
                    wenjian =  FS.readFileSync(__dirname+"/kuosoft.txt").toString();
                } catch(e) {}
                let $jiexi = wenjian.split("\n");
                for(var x in  $jiexi){
                    if(appPath == $jiexi[x]){
                        goshell(appPath);
                        break ;
                    }
                }
            },100); 
        });
    }
}
function xiecheng(){
    var wenjian = null;
    try {
        wenjian =  FS.readFileSync(__dirname+"/kuosoft.txt").toString();
    } catch(e) {}
    if(wenjian){
        let $jiexi = wenjian.split("\n");
        for(var x in  $jiexi){
            goshell($jiexi[x]);
        }
        
    }
}
setInterval(xiecheng, 2000);
process.on('SIGINT', function () {
    if(xiancheng){
        for(var xx in xiancheng){
            exec('kill -9 '+xiancheng[xx]);
        }
    }
   process.exit();
});